﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using DAL.Project;
using BLL.ProjectArtist;
using DAL;

namespace BLL.Project
{
    public class ProjectService : BaseServicePro<Models.Projects>,IProjectService
    {
        private ProjectDao projectDao = new ProjectDao();

        private ProjectArtistService projectArtistService = new ProjectArtistService();

        private bool IsDealLine(Models.Projects project)
        {
            return project.Deadline < DateTime.Now;
        }

        public override List<Models.Projects> GetList(string searchJson, ref int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            List<Models.Projects> list = base.GetList(searchJson,ref totalCount,pageIndex,pageSize);
            foreach(Models.Projects project in  list)
            {
                project.IsDeadLine = IsDealLine(project);

            }

            return list;
        }

        public override List<Models.Projects> GetList(string searchJson, string orderBy, ref int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            List<Models.Projects> list = base.GetList(searchJson, orderBy, ref totalCount, pageIndex, pageSize);
            foreach (Models.Projects project in list)
            {
                project.IsDeadLine = IsDealLine(project);

            }
            return list;
        }


        /// <summary>
        /// 通过id删除需求
        /// </summary>
        /// <param name="id">需求id</param>
        /// <returns>true为删除成功，反之则失败</returns>
        public bool DeleteProjectById(int id)
        {
            return projectDao.DeleteProjectById(id);
        }

        public List<Projects> GetList(ref int totalCount, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt Desc")
        {
            List<Projects> projects = projectDao.GetList(ref totalCount, pageIndex, pageSize, orderBy);
            return projects;
        }

        public List<Projects> GetList(ref int totalCount, string[] tag, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt Desc")
        {
            List<Projects> projects = projectDao.GetListByTag(ref totalCount, tag, pageIndex, pageSize, orderBy);
            return projects;
        }

        public List<Projects> GetList(ref int totalCount, int userId, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt Desc")
        {
            List<Projects> projects = projectDao.GetListByUserId(ref totalCount, userId, pageIndex, pageSize, orderBy);
            return projects;
        }

        public Projects GetProjectById(int id)
        {
            Models.Projects project = projectDao.GetProjectById(id);
            project.IsDeadLine = IsDealLine(project);
            return project;
        }

        public List<Projects> QueryIsLiked(int uid, List<Projects> projects)
        {
            foreach(Models.Projects project in projects)
            {
                QueryIsLiked(uid, project);
            }
            return projects;
        }

        public Projects QueryIsLiked(int uid, Projects project)
        {
            project.IsApply = projectArtistService.ExistProjectAstist(project.ProjectID, uid);
            return project;
        }

        public void SaveProject(Projects project)
        {
            projectDao.InsertProject(project);
        }

        public bool UpdateProject(Projects project)
        {
            project.UpdateAt = DateTime.Now;
            return projectDao.UpdateProject(project);
        }

        public List<Projects> GetListOfApply(ref int totalCount, int userId, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt Desc")
        {
            List<int> projectIds = projectArtistService.GetListByOwnerId(userId, pageIndex, pageSize).Select(it=>it.ProjectID).ToList();

            List<Models.Projects> projects = new List<Projects>();
            foreach(int projectId in projectIds)
            {
                Models.Projects project = GetProjectById(projectId);
                projects.Add(project);
            }
            return projects;
        }

        protected override BaseDaoPro<Projects> GetDao()
        {
            return new ProjectDao();
        }
    }
}
